package com.huonan.jb.tools.fileuse.fitdata;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author lxp
 * @Date 2023/4/7 下午1:23
 * @Version V1.0
 **/
@Slf4j
public class Test {
    public static void main(String[] args) throws JsonProcessingException {

        //filterNewData();
        mapping();
    }

    private static void mapping() {
        String source_path = "/Users/lxp/IdeaProjects/Study/MainTest/data/video-douyin" +
                ".com/3-kw_cooperateBinds/2023-09-08-pool-1-thread-1.json";
        String old_Path = "/Users/lxp/Documents/dev/数据需求/运营/数据修复/i7s字段修复/old/fit_kw_businessIndustry.json";
        String mappingPaht = "/Users/lxp/Documents/dev/数据需求/运营/数据修复/i7s字段修复/i7s_mapping_0828.json";
        String after_mapping = "/Users/lxp/IdeaProjects/Study/MainTest/data/video-douyin" +
                ".com/3-kw_cooperateBinds/after-mapping.json";

        List<String> source_list = FileUtil.readUtf8Lines(source_path);
        List<String> mapping = FileUtil.readUtf8Lines(mappingPaht);
        Map<String, JSONArray> map = new HashMap<>();
        for (String l : mapping) {
            JSONObject entries = JSONUtil.parseObj(l);
            String source_cooperateBinds = entries.getStr("kw_cooperateBinds");
            JSONArray source_businessIndustry = entries.getJSONArray("kw_businessIndustry");
            map.put(source_cooperateBinds, source_businessIndustry);
        }
        int HitCount = 0;
        for (String l : source_list) {
            JSONObject entries = JSONUtil.parseObj(l);
            String source_cooperateBinds = entries.getStr("kw_cooperateBinds");
            JSONArray kw_businessIndustry = map.get(source_cooperateBinds);
            if (kw_businessIndustry != null) {
                entries.remove("kw_cooperateBinds");
                entries.putOpt("kw_businessIndustry", kw_businessIndustry);
                HitCount++;
                String jsonStr = JSONUtil.toJsonStr(entries);
               // FileUtil.appendUtf8String(jsonStr+FileUtil.getLineSeparator(), after_mapping);
            }else {
                System.out.println(source_cooperateBinds);
            }

        }
        System.out.println("size:" + source_list.size());
        System.out.println("after mapping size:" + HitCount);
        System.out.println("===");
    }

    private static void filterNewData() {
        String resultPath = "/Users/lxp/Documents/dev/数据需求/运营/数据修复/i7s字段修复/new/fit_kw_businessIndustry2.json";
       // String old_Path = "/Users/lxp/Documents/dev/数据需求/运营/数据修复/i7s字段修复/old/delete_kw_businessIndustry.json";
        String old_Path = "/Users/lxp/Documents/dev/数据需求/运营/数据修复/i7s字段修复/old/fit_kw_businessIndustry.json";
        String new_Path = "/Users/lxp/IdeaProjects/Study/MainTest/data/video-douyin" +
                ".com/3-kw_cooperateBinds/after-mapping.json";
        List<String> old_List = FileUtil.readUtf8Lines(old_Path);
        List<String> new_List = FileUtil.readUtf8Lines(new_Path);

        Map<String, String> map = new HashMap<>();
        for (String l : new_List) {
            JSONObject entries = JSONUtil.parseObj(l);
            Object id = entries.get("_id");
            Object index = entries.get("_index");
            String key = index + "-" + id;
            map.put(key, l);
        }
        System.out.println("size:" + map.size());
        for (String l : old_List) {
            JSONObject entries = JSONUtil.parseObj(l);
            Object id = entries.get("_id");
            Object index = entries.get("_index");
            String key = index + "-" + id;
            map.remove(key);
        }
        System.out.println("去掉 size:" + map.size());
        List<String> afterRmList = map.values().stream().collect(Collectors.toList());
        FileUtil.appendUtf8Lines(afterRmList, resultPath);
    }
}
